# PandaCoder 版本更新脚本
# 使用方法: .\update-version.ps1

Write-Host "=========================================" -ForegroundColor Cyan
Write-Host "PandaCoder 版本更新工具" -ForegroundColor Cyan
Write-Host "=========================================" -ForegroundColor Cyan
Write-Host ""

# 读取 gradle.properties
$propsFile = "gradle.properties"
if (-not (Test-Path $propsFile)) {
    Write-Host "错误: 未找到 gradle.properties 文件" -ForegroundColor Red
    exit 1
}

$props = @{}
Get-Content $propsFile | ForEach-Object {
    if ($_ -match '^\s*([^#=]+?)\s*=\s*(.+?)\s*$') {
        $props[$matches[1]] = $matches[2]
    }
}

$pluginVersion = $props['pluginVersion']
$versionType = $props['versionType']
$releaseDate = $props['releaseDate']
$currentFeatures = $props['currentFeatures']

Write-Host "当前版本配置:" -ForegroundColor Yellow
Write-Host "  版本号: $pluginVersion"
Write-Host "  版本类型: $versionType"
Write-Host "  发布日期: $releaseDate"
Write-Host "  主要功能: $currentFeatures"
Write-Host ""

# 1. 更新 version.properties
Write-Host "正在更新 version.properties..." -ForegroundColor Green
$versionPropsPath = "src\main\resources\version.properties"
$versionPropsContent = @"
# Auto-generated version information
# DO NOT EDIT THIS FILE MANUALLY
# This file is generated by update-version.ps1 script
# To update version, edit gradle.properties file and run: .\update-version.ps1

version=$pluginVersion
versionType=$versionType
releaseDate=$releaseDate
currentFeatures=$currentFeatures
"@

[System.IO.File]::WriteAllText((Resolve-Path $versionPropsPath).Path, $versionPropsContent, [System.Text.Encoding]::UTF8)
Write-Host "  ✓ 已更新: $versionPropsPath" -ForegroundColor Green

# 2. 更新 README.md
Write-Host "正在更新 README.md..." -ForegroundColor Green
$readmePath = "README.md"
if (Test-Path $readmePath) {
    $readmeContent = [System.IO.File]::ReadAllText($readmePath, [System.Text.Encoding]::UTF8)
    $readmeContent = $readmeContent -replace 'https://img\.shields\.io/badge/Version-[\d.]+', "https://img.shields.io/badge/Version-$pluginVersion"
    [System.IO.File]::WriteAllText((Resolve-Path $readmePath).Path, $readmeContent, [System.Text.Encoding]::UTF8)
    Write-Host "  ✓ 已更新: $readmePath" -ForegroundColor Green
} else {
    Write-Host "  ✗ 未找到: $readmePath" -ForegroundColor Yellow
}

Write-Host ""
Write-Host "=========================================" -ForegroundColor Cyan
Write-Host "✓ 版本更新完成！" -ForegroundColor Green
Write-Host "=========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "已更新的文件:" -ForegroundColor Yellow
Write-Host "  1. src\main\resources\version.properties"
Write-Host "  2. README.md"
Write-Host ""
Write-Host "下一步操作:" -ForegroundColor Yellow
Write-Host "  1. 运行 .\gradlew clean build 重新构建项目"
Write-Host "  2. 提交更改到 Git: git add ."
Write-Host "     然后: git commit -m 'chore: update version to $pluginVersion'"
Write-Host "  3. 创建版本标签: git tag v$pluginVersion"
Write-Host "  4. 推送到远程: git push"
Write-Host "     然后: git push --tags"
Write-Host "=========================================" -ForegroundColor Cyan

